package com.yjps.house.service;

import com.yjps.house.dao.HistoryDao;
import com.yjps.house.dao.HouseDao;
import com.yjps.house.entity.HouseHistory;
import com.yjps.house.model.House;
import com.yjps.house.model.RecommendedHistory;
import com.yjps.system.dao.UserDao;
import com.yjps.system.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("recommendService")
public class RecommendService {
    @Autowired
    private HouseDao houseDao;

    @Autowired
    private HistoryDao historyDao;

    @Autowired
    private UserDao userDao;

    /**
     * �������Ƽ���Դ���ŵ꾭��δȷ�ϣ�
     * @param houseId
     * @param agentsId
     * @param time
     * @return houseId
     */
    @Transactional
    public int recommendHouse(int houseId,int agentsId,String time) {
        RecommendedHistory history=new RecommendedHistory();
        history.setHouseId(houseId);
        history.setTime(time);
        history.setAgentsId(agentsId);
        int historyId=historyDao.save(history);
        return historyId;
    }

    /**
     * �ŵ꾭���ȡ�ŵ��������Ƽ���Դ��¼
     * @param shopId
     * @param startRow
     * @param pageSize
     * @return
     */
    @Transactional
    public List<HouseHistory> allListByShopId(int shopId,int startRow, int pageSize) {
        //��ѯ���ŵ������о����˵ķ�����¼
        String hql="from t_house_recommend_history r" +
                "where r.agentsId IN(select a.id from t_agents a where a.shop_id := shopId) and p.valid := isValid";
        Map<String, Object> map = new HashMap<>();
        map.put("shopId", shopId);
        map.put("valid", 1);
        List<RecommendedHistory> rhs=historyDao.find(hql,map);

        //��ѯ��¼��Ӧ�ķ�Դ
        List<HouseHistory> ps=new ArrayList<>();
        for(RecommendedHistory rh:rhs){
            House house=houseDao.get(rh.getHouseId());
            if (house!=null){
                String hql2="from t_user u where u.id in (select user_id from t_agents a where a.id =: agentsId )";
                Map<String,Object> map2=new HashMap<>();
                map2.put("agentsId",rh.getAgentsId());
                List<User> users=userDao.find(hql2, map);
                if (!users.isEmpty()){
                    HouseHistory p=new HouseHistory(rh.getAgentsId(),users.get(0).getUsername(),rh.getTime(),house);
                    ps.add(p);
                }
            }
        }
        if (ps.size()<pageSize){
            return ps;
        }
        if ((ps.size())<=(pageSize+startRow)){
            return ps.subList(startRow-1,ps.size());
        }

        return ps.subList(startRow-1,startRow+pageSize-1);
    }

    /**
     * �ŵ꾭���ȡ���ŵ���δȷ�ϵ��Ƽ���Դ��¼
     * @param shopId
     * @param startRow
     * @param pageSize
     * @return
     */
    public List<HouseHistory> unRecommendListByShopId(int shopId,int startRow, int pageSize){
        List<HouseHistory> ps=allListByShopId(shopId, startRow, pageSize);
        for (HouseHistory p : ps ){
            if (p.getHouse().getIsBoutique()==1){
                ps.remove(p);
            }
        }
        return ps;
    }

}
